const mongoose = require('../connect');
const moment = require('moment')
const md5 = require('md5');

//创建文档格式
let schema = new mongoose.Schema({
    username: {
        type: String,
        required: true
    },
    pwd: {
        type: String,
        required: true
    },
    profile: {
        type: String,
        required: true
    },
    create_time: {
        type: String,
        default: () => moment().format('YYYY-MM-DD HH:mm:ss')
    },
}, { versionKey: false })

// 创建模型对象
let userModel = mongoose.model('users', schema);


// 初始化数据代码
(async function() {

    let users = await userModel.findOne({ username: 'admin' })

    if (!users) {
        // 创建超级管理员用户
        userModel.create({
            username: "admin",
            pwd: md5('111111'),
            profile: "/upload/admin.png"
        })

        for (let i = 0; i < 30; i++) {
            userModel.create({
                username: "user_" + i,
                pwd: md5('111111'),
                profile: "/upload/admin.png"
            })
        }
    }


})()


module.exports = userModel;